home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 139_01 / ked2.c < prev    next >
Text File  |  1985-08-19  |  24KB  |  707 lines

  1. /*
  2. TITLE:        Karel The Robot;
  3. VERSION:    1.0;
  4. DATE:        09/27/1984;
  5. DESCRIPTION:
  6.     "Part 2 of source code for syntax-directed editor,
  7.      KED. Other functions.";
  8. KEYWORDS:    Editor, syntax;
  9. SYSTEM:        CP/M 2+;
  10. FILENAME:    KED2.C
  11. AUTHORS:    Linda Rising;
  12. COMPILERS:    BDS C;
  13. REFERENCES:
  14.     AUTHOR: Richard E. Pattis;
  15.     TITLE: "KAREL the Robot: A Gentle Introduction
  16.         to the Art of Programming";
  17.     CITATION: "";
  18. ENDREF
  19. */
  20.  
  21.  
  22. #include <bdscio.h>
  23. #include <ked.h>
  24.  
  25.  
  26. readin(w)   /* find location of command in comm array or symbol table */
  27. char w[];
  28. {
  29.           int c,i,l,m;
  30.  
  31.           do {
  32.             inputs(w);
  33.             if ((m = match(w,comm,MAXCOMM)) != NOMATCH)
  34.                return m;              
  35.             if (m == NOMATCH && last != first && p != pr) {
  36.                l = pr->lastin;
  37.                if (l == 15) 
  38.                    l = last;
  39.                for (i = first; i != l; i = new[i].nextin)
  40.                     if ((c = strcomp(w,new[i].defname)) == 0) 
  41.                        return ( -i - 1);
  42.             }          
  43.             printf("INVALID COMMAND. TYPE MN TO SEE MENU.\n?");
  44.           } while (m == NOMATCH);
  45. }
  46.  
  47.  
  48.  
  49. menu()           /* print updated menu */
  50. {
  51.           int i;
  52.  
  53.           printf("THE FOLLOWING IS A LIST OF VALID ");
  54.           printf("COMMANDS FOR THIS EDITOR.\n\n");
  55.           printf("  INSTRUCTIONS\n");   
  56.           printf("m          move\n");
  57.           printf("tl         turnleft\n");
  58.           printf("pi         pickbeeper\n");
  59.           printf("pu         putbeeper\n");
  60.           printf("to         turnoff\n");
  61.           printf("b          BEGIN\n");
  62.           printf("bx         BEGINNING-OF-EXECUTION\n");
  63.           printf("nd         END\n");
  64.           printf("ndc        end construct\n");
  65.           printf("ndx        END-OF-EXECUTION\n");
  66.           printf("if         IF\n");
  67.           printf("el         ELSE\n");
  68.           printf("it         ITERATE\n");
  69.           printf("wh         WHILE\n");
  70.           printf("df         DEFINE-NEW-INSTRUCTION\n");
  71.       printf("\nTYPE ANY KEY TO CONTINUE.\n");
  72.       getchar();
  73.           if (last != first) {   /* any valid sym tab entries? */
  74.            printf("\n  USER-DEFINED COMMANDS\n");
  75.            for (i = first; i != last; i = new[i].nextin)
  76.                 printf("%s\n", new[i].defname);  
  77.           }
  78.           printf("\n  EDIT COMMANDS\n");      
  79.           printf("n          move pointer to next instruction\n");
  80.           printf("nc         move pointer to next construct\n");
  81.           printf("n-         move pointer to previous instruction\n");
  82.           printf("n--        move pointer back 10 lines\n"); 
  83.           printf("d          delete current instruction\n");
  84.       printf("c          print current line\n");
  85.           printf("p          print next 20 lines\n");
  86.           printf("i          insert new instruction before current one\n");
  87.           printf("t          move pointer to the first line\n");
  88.           printf("l          list the rest of the program\n");
  89.           printf("s          save the program\n");
  90.           printf("mn         print an updated menu\n");
  91.           printf("q          quit the edit program\n");
  92.       printf("\nTYPE ANY KEY TO CONTINUE.\n");
  93.       getchar();
  94.           printf("\n  TESTS\n");
  95.           printf("n          facing-north\n");
  96.           printf("nn         not-facing-north\n");
  97.           printf("s          facing-south\n");
  98.           printf("ns         not-facing-south\n");
  99.           printf("e          facing-east\n");
  100.           printf("ne         not-facing-east\n");
  101.           printf("w          facing-west\n");
  102.           printf("nw         not-facing-west\n");
  103.           printf("c          next-to-a-beeper (beeper on corner)\n");
  104.           printf("nc         not-next-to-a-beeper\n");
  105.           printf("b          any-beepers-in-beeper-bag\n");
  106.           printf("nb         no-beepers-in-beeper bag\n");
  107.           printf("f          front-is-clear\n");
  108.           printf("nf         front-is-blocked\n");
  109.           printf("l          left-is-clear\n");
  110.           printf("nl         left-is-blocked\n");
  111.           printf("r          right-is-clear\n");
  112.           printf("nr         right-is-blocked\n");
  113. }
  114.  
  115. createtree()                      /* reads information from old file1 */
  116. {
  117.            int i,in,d,fn,fs,b,t,l;
  118.            struct tnode *alloc();
  119.  
  120.            fscanf(ibuf1, "%d %d %d %d %d %d %d %d",
  121.                   &i,&in,&d,&fn,&fs,&b,&t,&l);
  122.            pr = NULL;
  123.            p = alloc(24);                 
  124.            tree(i,in,d,fn,fs,b,t,l);              /* store first node */
  125.            root = p;
  126.            p = root->sub = alloc(24);
  127.            pr = root;
  128.            fscanf(ibuf1, "%d %d %d %d %d %d %d %d",
  129.                   &i,&in,&d,&fn,&fs,&b,&t,&l);
  130.            while (i != NDX && i != CPMEOF && i != EOF) { 
  131.                createnode(ibuf1,i,in,d,fn,fs,b,t,l);    
  132.                fscanf(ibuf1, "%d %d %d %d %d %d %d %d",
  133.                       &i,&in,&d,&fn,&fs,&b,&t,&l);
  134.            }
  135.            endcons();
  136.            tree(i,in,d,fn,fs,b,t,l);
  137.            pr = p;
  138.            fclose(ibuf1);        
  139. }
  140.  
  141.  
  142. createnode(ibuf1,i,in,d,fn,fs,b,t,l)    /* restores nodes for parse tree */
  143. FILE *ibuf1;
  144. int i,in,d,fn,fs,b,t,l;
  145. {
  146.           int j;
  147.           char w[MAXLINE];
  148.           struct tnode *allocp();
  149.  
  150.           if (d == NEWINSTR && pr->instr != DEF) {
  151.               i = reloc[i];                   /* set up array to convert */
  152.               j = 0;                              /* old symb tab to new */
  153.               while (new[i].loc[j] != NULL)
  154.                      j++;
  155.               new[i].loc[j] = p;
  156.               new [i].loc[++j] = NULL;
  157.           if (l != 15)
  158.          l = last;
  159.               tree(i,in,d,fn,fs,b,t,l);
  160.               p = p->next = allocp();
  161.           } else switch (i) { 
  162.                  case 0:            /* move */
  163.                  case 1:            /* turnleft */
  164.                  case 2:            /* pickbeeper */
  165.                  case 3:            /* putbeeper */
  166.                  case 4:            /* turnoff */
  167.                     while (in < pr->indent)
  168.                            endcons();
  169.             if (l != 15)
  170.                l = last;
  171.                     tree(i,in,d,fn,fs,b,t,l);
  172.                     p = p->next = allocp();
  173.                     break;
  174.                 case 5:             /* BEGIN */
  175.                 case 7:             /* BEGINNING-OF-EXECUTION */
  176.                 case 13:            /* ELSE */
  177.            if (l != 15)
  178.               l = last;
  179.                    tree(i,in,d,fn,fs,b,t,l);
  180.                    p = p->sub = allocp();
  181.                    break;
  182.                case 8:              /* END */
  183.           if (l != 15)
  184.              l = last;
  185.                   tree(i,in,d,fn,fs,b,t,l);
  186.                   p = p->next = allocp();
  187.                   endcons();
  188.                   break;
  189.                case 11:             /* IF */
  190.                   while (in < pr->indent)
  191.                       endcons();
  192.           if (l != 15)
  193.              l = last;
  194.                   tree(i,in,d,fn,fs,b,t,l);
  195.                   p = p->sub = allocp();
  196.                   fscanf(ibuf1,"%d %d %d %d %d %d %d %d",
  197.                          &i,&in,&d,&fn,&fs,&b,&t,&l);         /* get test */
  198.           if (l != 15)
  199.              l = last;
  200.                   tree(i,in,d,fn,fs,b,t,l);
  201.                   p = p->sub = allocp();
  202.                   fscanf(ibuf1, "%d %d %d %d %d %d %d %d",
  203.                          &i,&in,&d,&fn,&fs,&b,&t,&l);         /* get THEN */
  204.           if (l != 15)
  205.              l = last;
  206.                   tree(i,in,d,fn,fs,b,t,l);
  207.                   p = p->sub = allocp();
  208.                   break;
  209.                case 14:               /* ITERATE */
  210.                case 15:               /* WHILE */
  211.                   while (in < pr->indent)
  212.                       endcons();
  213.           if (l != 15)
  214.              l = last;
  215.                   tree(i,in,d,fn,fs,b,t,l);
  216.                   p = p->sub = allocp();
  217.                   fscanf(ibuf1,"%d %d %d %d %d %d %d %d",
  218.                          &i,&in,&d,&fn,&fs,&b,&t,&l);      /* get test or */
  219.           if (l